<template>
  <div class="login">
    <el-card class="login-box">
      <div class="login-title">后台管理系统通用模板</div>
      <el-form
        ref="loginForm"
        :model="form"
        :rules="rules"
        class="login-form"
        auto-complete="off"
      >
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="用户名" />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="密码"
            show-password
          />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="form.remember">记住密码</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            @click="login"
            style="width: 100%"
            :disabled="disabled"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { userStore } from '@/store/user'

const router = useRouter()
const user = userStore()
const disabled = ref(false)
const loginForm = ref<any>()
const form = reactive({
  username: '',
  password: '',
  remember: false,
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    {
      min: 3,
      max: 10,
      message: '长度在 3 到 10 个字符',
    },
  ],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
}

watchEffect(() => {
  // 符合条件时，才能点击登录按钮
  if (form.username && form.password) {
    loginForm.value.validate((valid: boolean) => {
      if (valid) {
        disabled.value = false
      } else {
        disabled.value = true
      }
    })
  } else {
    disabled.value = true
  }
})
const login = () => {
  loginForm.value.validate((valid: boolean) => {
    if (valid) {
      // 模拟登录成功
      user.setUserInfo({
        userName: form.username,
        password: form.password,
        token: 'token',
        auth: ['ee'],
      })
      localStorage.setItem('token', 'token')
      router.push('/app')
      if (form.remember) {
        localStorage.setItem(
          'remember',
          JSON.stringify({
            userName: form.username,
            password: form.password,
          })
        )
      }
    } else {
      ElMessage.error('请填写完整信息')
      return false
    }
  })
}

onMounted(() => {
  const remember = localStorage.getItem('remember')
  if (remember) {
    const { userName, password } = JSON.parse(remember)
    form.username = userName
    form.password = password
    form.remember = true
  }
})
</script>

<style scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.login-box {
  width: 400px;
  padding: 20px;
}

.login-title {
  font-size: 20px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 20px;
}

.login-form {
  margin-top: 20px;
}
</style>
